package com.ruoyi.adrss;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @description: 百度地图地址
 * @author: xly
 * @date: 2021-12-17
 */
public class Http {
    public static void main(String[] args){
        doGet("https://api.map.baidu.com/reverse_geocoding/v3/?ak=GbDXkQOVHGH3LNiwpYcj4DlY9YjUk1LW&output=json&coordtype=wgs84ll&extensions_town=true&location=31.225696563611,121.49884033194");
    }

    public static String doGet(String url){
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        String result = "";
        try{
            //通过默认配置创建一个httpClient实例
            httpClient = HttpClients.createDefault();
            //创建httpGet远程连接实例
            HttpGet httpGet = new HttpGet(url);
            //httpGet.addHeader("Connection", "keep-alive");
            //设置请求头信息
            httpGet.addHeader("Accept", "application/json");
            //配置请求参数
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(5000) //设置连接主机服务超时时间
                    .setConnectionRequestTimeout(5000)//设置请求超时时间
                    .setSocketTimeout(5000)//设置数据读取超时时间
                    .build();
            //为httpGet实例设置配置
            httpGet.setConfig(requestConfig);
            //执行get请求得到返回对象
            response = httpClient.execute(httpGet);
            //通过返回对象获取返回数据
            HttpEntity entity = response.getEntity();
            //通过EntityUtils中的toString方法将结果转换为字符串，后续根据需要处理对应的reponse code
            result = EntityUtils.toString(entity,"UTF-8");
            Adrss adrss = new Gson().fromJson(result, new TypeToken<Adrss>() {
            }.getType());
            if(adrss != null
                    && adrss.getStatus() == 0
                    && adrss.getResult() != null
                    && adrss.getResult().getAddressComponent() != null){
                AdrssComponent component = adrss.getResult().getAddressComponent();
                StringBuilder sb = new StringBuilder();
                sb.append(component.getDistrict());
                sb.append(component.getTown());
                sb.append(component.getStreet());
                sb.append(component.getStreet_number());
                return sb.toString();
            }
            return "";
        }catch (ClientProtocolException e){
            e.printStackTrace();
        }catch (IOException ioe){
            ioe.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //关闭资源
            if(response != null){
                try {
                    response.close();
                }catch (IOException ioe){
                    ioe.printStackTrace();
                }
            }
            if(httpClient != null){
                try{
                    httpClient.close();
                }catch (IOException ioe){
                    ioe.printStackTrace();
                }
            }
        }
        return result;
    }
}
